<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final
 * @category    Models
 * @see 		Articles
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Articles_model extends CI_Model {
	public function __construct()
	{
		parent::__construct();
	}

	public function show($uri, $num_rows = false)
	{
		$this->db->query('SET NAMES "LATIN1"');
		if ($uri['limit'] == '') $uri['limit'] = 50;
		$this->db->from('articles AS a');
		if ($uri['orderby'] != '') $this->db->where('a.active', 1);
		if ($uri['orderby'] == 'order_top') $this->db->where('a.menu_top', 1);
		if ($uri['orderby'] == 'order_bottom') $this->db->where('a.menu_bottom', 1);
		if ($uri['orderby'] == 'order_footer') $this->db->where('a.menu_footer', 1);
		if ($uri['q'] != '')
		{
			$this->db->where(sprintf('(a.name_vn LIKE "%%%s%%" OR a.name_en LIKE "%%%s%%" OR a.tags LIKE "%%%s%%" OR a.keyword LIKE "%%%s%%")', $uri['q'], $uri['q'], $uri['q'], str_replace(' ', '-', $uri['q'])));
		}
		if ($uri['cat'] > 0)
		{
			$this->db->where('a.cat', $uri['cat']);
		}
		$this->db->where('a.deleted', $uri['deleted']);
		if ($num_rows)
		{
			$this->db->select('a.id');
			return $this->db->get()->num_rows();
		}
		else
		{
			$last_login = $GLOBALS['user']['last_login'];
			$this->db->select('a.*, COUNT(m.id) AS comment, c.name_vn AS cat, IF ('.sprintf('a.date_added >= %1$s', $this->db->escape($last_login)).', 1, 0) as isnew', false);
			$this->db->group_by('id');
			$this->db->join('comments as m', 'a.id = m.article AND m.reply = 0', 'left');
			$this->db->join('article_categories AS c', 'c.id = a.cat', 'left');
			if ($uri['orderby'] == '') $uri['orderby'] = 'id';
			if ($uri['ordermode'] == '') $uri['ordermode'] = 'desc';
			$this->db->order_by($uri['orderby'].' '.$uri['ordermode']);
			$this->db->limit($uri['limit'] + 1, $uri['rowstart']);
			$query = $this->db->get();
			if ($query->num_rows() > 0)
			{
				return $query->result_array();
			}
			else
			{
				return false;
			}
		}
	}

	public function filter_category_list() {
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->from('article_categories AS c');
		$this->db->select('c.id, c.parent, c.name_vn, COUNT(s.parent) AS chirld, COUNT(a.id) AS product', false);
		$this->db->group_by('id');
		$this->db->join('article_categories as s', 'c.id = s.parent', 'left');
		$this->db->join('articles as a', 'c.id = a.cat', 'left');
		$this->db->order_by('c.sort_order asc');
		$query = $this->db->get();
		$data = array();
		foreach ($query->result_array() as $val)
		{
			if ($val['chirld'] || $val['product']) $data[] = $val;
		}
		return $data;
	}

	public function info($id)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->where('id', $id);
		$query = $this->db->get('articles');
		if ($query->num_rows() > 0)
		{
			return $query->row_array();
		}
		else
		{
			$fields = $this->db->field_data('articles');
			$default_data = array();
			foreach ($fields as $field)
			{
				$default_data[$field->name] = $field->default;
			}
			return $default_data;
		}
	}

	public function process($data, $id = '')
	{
		if (!is_array($data))
		{
			return false;
		}
		$this->db->query('SET NAMES "LATIN1"');
		if ($id > 0)
		{
			$this->db->where('id', $id);
			$this->db->update('articles', $data);
			write_log('update_articles', $data['name_vn']);
		}
		else
		{
			$this->db->insert('articles', $data);
			write_log('add_articles', $data['name_vn']);
		}
		return true;
	}

}

/* End of file articles_model.php */
/* Location: ./application/models/articles_model.php */